package com.google.code.hostsman;

import java.util.List;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.code.hostsman.NewEntryDialogFragment.NewEntryDialogListener;
import com.google.code.hostsman.db.DataSource;
import com.google.code.hostsman.db.DomainIPPair;
import com.google.code.hostsman.db.Group;

public class GroupActivity extends FragmentActivity implements NewEntryDialogListener {

	public static final String INTENT_GROUP_ID = GroupActivity.class.getName()
			+ ".GroupID";
	private long groupId;

	private DataSource datasource;
	private ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_group);
		// Show the Up button in the action bar.
		setupActionBar();

		Intent myIntent = getIntent();
		groupId = myIntent.getLongExtra(INTENT_GROUP_ID, -1);

		datasource = new DataSource(this);
		datasource.open();
		
		Group group=datasource.getGroup(groupId);
		setTitle(String.format(getResources().getString(R.string.title_activity_group),group.getName()));

		List<DomainIPPair> values = datasource.getAllPairsInGroup(groupId);

		listView = (ListView) findViewById(android.R.id.list);

		final PairArrayAdapter adapter = new PairArrayAdapter(this, values);
		listView.setAdapter(adapter);
	}
	
	private class PairArrayAdapter extends ArrayAdapter<DomainIPPair> {
		private List<DomainIPPair> values;
		private Context context;

		public PairArrayAdapter(Context context, List<DomainIPPair> values) {
			super(context, R.layout.pair_item, values);
			this.context = context;
			this.values = values;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View rowView = LayoutInflater.from(context).inflate(
					R.layout.pair_item, parent, false);
			
			TextView ipView = (TextView) rowView
					.findViewById(R.id.ip);
			TextView domainView = (TextView) rowView
					.findViewById(R.id.domain);
			
			ipView.setText(values.get(position).getIp());
			domainView.setText(values.get(position).getDomain());
			
			return rowView;
		}

		public long getItemId(int position) {
			return values.get(position).getId();
		}

		public boolean hasStableIds() {
			return true;
		}
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.group, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		case R.id.action_new_entry:
			showNewEntryDialog();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void showNewEntryDialog() {
		NewEntryDialogFragment fragment = new NewEntryDialogFragment();
		fragment.show(getSupportFragmentManager(), "new_entry");
	}

	
	@Override
	protected void onResume() {
		datasource.open();
		super.onResume();
	}

	@Override
	protected void onPause() {
		datasource.close();
		super.onPause();
	}

	@Override
	public void onDialogEntryAdd(DialogFragment dialog, DomainIPPair pair) {
		if (pair.getDomain().length() > 0 && pair.getIp().length()>0) {
			pair.setGroupId(groupId);
			datasource.createPair(pair);
			PairArrayAdapter adapter = (PairArrayAdapter) listView
					.getAdapter();
			adapter.add(pair);
		}
	}
}
